package com.koron.common.core.config;

import com.alibaba.fastjson.JSONObject;

import java.util.Objects;

/**
 * @ClassName CustomInheritableThreadLocal
 * @description 重写java.lang.InheritableThreadLocal#childValue方法.
 * 这里自定义一个MyInheritableThreadLocal类，实现对象的拷贝,避免父子线程共用同一引用导致线程变量被修改
 * @Author zhouj
 * @Date 2022/6/1 9:19
 */
public class CustomInheritableThreadLocal<T> extends InheritableThreadLocal<T> {

    @Override
    protected T childValue(T parentValue) {
        if (Objects.isNull(parentValue)) {
            return parentValue;
        }
        String s = JSONObject.toJSONString(parentValue);
        return (T) JSONObject.parseObject(s, parentValue.getClass());
    }
}
